﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using WXApi.Model;
using WxTenpay.Common;
using WxTenpay.Service;
using WxTenpay.Service.Model;

namespace WXApi.Controllers
{
    /// <summary>
    /// 项目公共接口
    /// </summary>
    /// 
    [Route("[controller]/[action]")]
    [ApiController]
    public class CommonApiController : ReqDataControllerBase
    {

        private readonly ISysLog syslog;
        /// <summary>
        ///  公共上传文件
        /// </summary>
        private string path = "/wwwwroot/files";
        public CommonApiController(ISysLog _syslog)
        {
            syslog = _syslog;
        }

        /// <summary>
        /// 公共上传文件接口
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public object AddUpload(string guid, IFormFile file)
        {
            try
            {
                string respath = "";//返回的数据列表

                #region 保存文件


                if (file.Length > 0)
                {


                    if (file.Name == guid)
                    {

                        var result = Thumbnail.ApiUploadFile(file, path + "/" + DateTime.Now.ToString("yyyyMMdd"), guid);
                        if (result == "undefined")
                            return Error("头衔证明文件异常！请重新上传！");
                        respath = result;
                    }

                }
                return Success(new { path = respath });//返回上传文件

                #endregion



            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new  SysLogEntity { Action = "CommonApi/AddUpload", Content = ex.ToString(), Name = "公共上传文件接口" };
                return ErrorLog(model);
            }
        }

        /// <summary>
        /// 获取图形验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetVerification()
        {
            try
            {
                var v = Utils.CreateBase64Imgage();

                var data = new
                {
                    vcode = DESEncrypt.MD5Encrypt(v.Item1),
                    images = v.Item2

                };
                return Success(data);


            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "CommonApi/GetVerification", Content = ex.ToString(), Name = "获取图形验证码" };
                return ErrorLog(model);
              
            }



        }


        public ActionResult GetTest() {
            try
            {
                return Success("测试成功！");
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "CommonApi/AddUpload", Content = ex.ToString(), Name = "公共上传文件接口" };
                return ErrorLog(model);
            }
        
        }

    }
}
